package com.group01.community.controller;


import com.group01.community.entity.SysMenu;
import com.group01.community.entity.vo.SysMenuVo;
import com.group01.community.service.SysMenuService;
import com.group01.community.utils.ResponseResult;
import com.group01.community.utils.TreeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author iuoly
 * @since 2022-07-04
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/sysMenu")
public class  SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation("查询所有三级菜单（根据用户名==>左侧导航栏显示的菜单权限）")
    @GetMapping("menus/{username}")
    public ResponseResult<List<SysMenuVo>> getMenuByUsername(@PathVariable String username){
//        System.out.println(username);
        return ResponseResult.ok(sysMenuService.getMenusByUserName(username));
    }

    @ApiOperation("查询所有分级菜单(四级菜单+模糊查询)")
    @GetMapping("/menu/")
    public ResponseResult<List<SysMenuVo>> getMenuList(String menuName, String status){
        List<SysMenuVo> menuList = sysMenuService.getMenuList(menuName, status);
        //构建树
        List<SysMenuVo> menuLists = new TreeUtils(menuList).buildTree();
        return ResponseResult.ok(menuLists);
    };

    @ApiOperation("新增菜单数据")
    @PostMapping ("/menu")
    public ResponseResult<String> addMenu(@RequestBody SysMenu sysMenu){
        LocalDateTime creatTime = LocalDateTime.now();
        sysMenu.setCreateTime(creatTime);
        System.out.println(sysMenu);
        sysMenuService.addMenu(sysMenu);
        return ResponseResult.ok("ok");
    }

    @ApiOperation("更新菜单数据")
    @PutMapping("/menu")
    public ResponseResult<String>updateMenu(@RequestBody SysMenu sysMenu){
        sysMenuService.updateMenu(sysMenu);
        LocalDateTime updateTime = LocalDateTime.now();
        sysMenu.setUpdateTime(updateTime);
        sysMenuService.updateMenu(sysMenu);
        return ResponseResult.ok("ok");
    }

    @ApiOperation("删除菜单数据（根据菜单id）")
    @DeleteMapping ("/menu/{menuId}")
    public ResponseResult<String> deleteMenu( @PathVariable Long menuId){
        sysMenuService.deleteMenu(menuId);
        return ResponseResult.ok("ok");
    }

    @ApiOperation("根据菜单名称查询菜单信息")
    @GetMapping ("/menus")
    public ResponseResult<SysMenu> getMenu(String menuName){
        return ResponseResult.ok(sysMenuService.getMenuByMenuName(menuName));
    }
}

